package com.example.swaggerDemo.service;

import com.example.swaggerDemo.Dao.WuliaoDao;
import com.example.swaggerDemo.entity.WuliaoEntity;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Resource
@Service

public class WuliaoService implements WuliaoDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    @Override
    public int insert(@RequestParam("openid") String openid, @RequestParam("nickname") String nickname
            , @RequestParam("type") String type,@RequestParam("amount") String amount,@RequestParam("phone") String phone, @RequestParam("address") String address) {
        String createtime= df.format(new Date());
        String status="0";
        String sql = "insert into wuliao (openid,nickname,type,amount,createtime,status,phone,address)" +
                "values(?,?,?,?,?,?,?,?)";
        return jdbcTemplate.update(sql,openid,nickname,type,amount,createtime,status,phone,address);
    }


    /**
     * 分页查询物料申请
     * @param userid

     * @param pagindex
     * @param pagesize
     * @return
     * @throws SQLException
     */
    @Override
    public List<WuliaoEntity> findUserId(@RequestParam("userid") String userid,
                                               @RequestParam("pageindex") int pagindex,
                                               @RequestParam("pagesize") int pagesize
    ) throws SQLException {
        String sql = "select * from wuliao WHERE  openid= '" + userid + "'" +
                " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        List<WuliaoEntity> dto = jdbcTemplate.query(sql, new BeanPropertyRowMapper(WuliaoEntity.class));
        return dto;
    }

    /**
     * 查询物料详情
     * @param id
     * @return
     * @throws SQLException
     */
    @Override
    public  WuliaoEntity findByid(@RequestParam("id") String id) throws SQLException {
        String sql = "select * from wuliao WHERE  id= '" +  id + "'";
        WuliaoEntity dto=null;
        try{
            dto = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<WuliaoEntity>(WuliaoEntity.class));
        }
        catch(Exception ex){

        }
        return dto;
    }

    /**
     * 管理员查看物料申请
     * @param pagindex
     * @param pagesize
     * @return
     * @throws SQLException
     */
    @Override
    public List<WuliaoEntity> findadmin(@RequestParam("status") String status,@RequestParam("pageindex") int pagindex, @RequestParam("pagesize") int pagesize) throws SQLException {
        String sql = "select * from wuliao  WHERE  status= '" +  status + "'"+
                " order by id DESC " + "LIMIT "+ pagindex +","+pagesize;
        List<WuliaoEntity> dto = jdbcTemplate.query(sql, new BeanPropertyRowMapper(WuliaoEntity.class));
        return dto;
    }

    //状态:0=未发货,1=已发货

    /**
     * 修改物料发货状态
     * @param id
     * @param status
     * @return
     */
    @Override
    public int updatewuliaoType(@RequestParam("id") String id, @RequestParam("status") String status) {
        String sql = "update  wuliao set status=? where id=?";
        return jdbcTemplate.update(sql,status,id);
    }
}
